package com.qiaofang.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.Charset;

@Slf4j
public class HttpUtil {

    private static final String CHARSET = "UTF-8";
    private static final String JSON = "application/json";

    public static String httpClientUploadFile(String url, String type, MultipartFile file) {
        CloseableHttpClient httpClient = HttpClientPool.getClient();
        String result = "";
        try {
            String fileName = file.getOriginalFilename();
            HttpPost httpPost = new HttpPost(url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            // 文件流
            builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);
            // 类似浏览器表单提交，对应input的name和value
            builder.addTextBody("filename", fileName);
            if (StringUtil.isNotBlank(type)) {
                builder.addTextBody("type", type);
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            // 执行提交
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(responseEntity, CHARSET);
            }
        } catch (Exception e) {
            log.error("上传文件报错: {}", e);
        }
        return result;
    }

    public static String doGet(String url) {
        String result = null;
        try {
            // 创建默认的httpClient实例.
            CloseableHttpClient httpClient = HttpClientPool.getClient();
            HttpGet httpGet = new HttpGet(url);
            // 执行请求
            HttpResponse response = httpClient.execute(httpGet);
            if (response != null) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, CHARSET);
                }
            }
        } catch (Exception e) {
            log.error("isExistsInM2 error: {}",e);
        }
        return result;
    }

    public static String doPostV10(String url, String jsonBody, String appId, String appKey, String companyUuid) throws Exception {
        String result = null;
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost;
        httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("X-AUTH-APP-ID", appId);
        httpPost.setHeader("X-AUTH-KEY", appKey);
        httpPost.setHeader("companyUuid", companyUuid);
        StringEntity se = new StringEntity(jsonBody, Charset.forName(CHARSET));
        se.setContentType(JSON);
        httpPost.setEntity(se);
        HttpResponse response = client.execute(httpPost);
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, CHARSET);
            }
        }
        return result;
    }

}
